package cn.java89.userservice.web;

import cn.java89.userservice.mybatis.entity.AjaxResult;
import cn.java89.userservice.mybatis.entity.Page;
import cn.java89.userservice.mybatis.entity.Student;
import cn.java89.userservice.mybatis.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController
@RequestMapping(("/student"))
public class StuController {
    @Autowired
    private IStudentService studentService;

    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable long id) {
        Student s = studentService.getById(id);
//        int i = 4 / 0;
//        try {
//            Thread.sleep(300000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return AjaxResult.obj(s);
    }

    @PostMapping("/login") //{"phone":"xxx","pass":"fff"}
    public AjaxResult login(String phone, String pass) {
        //System.out.println("??????" + phone + "," + pass);
        Student s = studentService.login(phone, pass);
        return AjaxResult.obj(s);
    }

    @GetMapping("/search")
    public Page<Student> search(int pageNo,
                                int pageSize,
                                String sname,
                                String qq,
                                String phone,
                                Date enterStart, Date enterEnd) {
        return studentService.search(pageNo, pageSize, sname, qq, phone, enterStart, enterEnd);
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        //所有的Date类型用一种格式
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
        //binder.registerCustomEditor(Date.class,"birthday", new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd
        // hh:mm"), true));
    }
}
